<?php
require('SendSms.php');

$goods = $_POST['goods'];
$name = $_POST['name'];
$uid = $_POST['uid'];
$phone = $_POST['phone'];
$sf = $_POST['sf'];
$city = $_POST['city'];
$xian = $_POST['xian'];
$dizhi = $_POST['dizhi'];
$oderid = $_POST['oderid'];
$time = $_POST['time'];
$channel = $_POST['channel'];
$price = $_POST['price'];

include_once("../untils/conn.php");
if ($con) {
    mysqli_query($con, "set names utf8");
    $data_query = mysqli_query($con, "select * from oder where uid='$uid'");
    $count = mysqli_num_rows($data_query);

    if ($count > 5) {
        // 用户已经申请过了，返回状态码1和提示信息
        $response = array('status' => 1, 'msg' => '您申请的订单已超限，请联系客服');
        echo json_encode($response);
    } else {
        // 判断$channel变量是否为空，如果为空则将其赋值为null
        if(empty($channel)){
            $channel = 0;
        }
        $data_insert = mysqli_query($con, "insert into oder(goods,name,uid,phone,sf,city,xian,dizhi,oderid,time,price,channel) values('$goods','$name','$uid','$phone','$sf','$city','$xian','$dizhi','$oderid','$time','$price','$channel')");
        
        if ($data_insert) {
            // 申请成功，返回状态码0和提示信息
            $response = array('status' => 0, 'msg' => '申请成功');
            echo json_encode($response);
            
            // 判断$status是否为0，如果是则执行$sendSms()函数
            if ($response['status'] == 0) {
                sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
            }
        } else {
            // 信息不全，返回状态码-1和提示信息
            $response = array('status' => -1, 'msg' => '信息不全，请输入全部信息');
            echo json_encode($response);
            // echo "Error updating record: " . $con->error;
        }
    }
}
